home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz⁄THINK C / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  9.4 KB  |  378 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     Kibitz
  5. ** File:        menu.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  19. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  21.  
  22. #ifndef __DESK__
  23. #include <Desk.h>
  24. #endif
  25.  
  26. #ifndef __ERRORS__
  27. #include <Errors.h>
  28. #endif
  29.  
  30. #ifndef __MEMORY__
  31. #include <Memory.h>
  32. #endif
  33.  
  34. #ifndef __MENUS__
  35. #include <Menus.h>
  36. #endif
  37.  
  38. #ifndef __TEXTEDITCONTROL__
  39. #include "TextEditControl.h"
  40. #endif
  41.  
  42. #ifndef __TOOLUTILS__
  43. #include <ToolUtils.h>
  44. #endif
  45.  
  46. #ifndef __UTILITIES__
  47. #include "Utilities.h"
  48. #endif
  49.  
  50.  
  51.  
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. extern Boolean    gQuitApplication;
  57. extern Boolean    gHasAppleEvents;
  58. extern Boolean    gHasPPCToolbox;
  59. extern Boolean    gSendToSelf;
  60. extern short    gReplyMode;
  61.  
  62.  
  63.  
  64. /*****************************************************************************/
  65. /*****************************************************************************/
  66.  
  67.  
  68.  
  69. /* Enable and disable menus based on the current state.  The user can only
  70. ** select enabled menu items.  We set up all the menu items before calling
  71. ** MenuSelect or MenuKey, since these are the only times that a menu item can
  72. ** be selected.  Note that MenuSelect is also the only time the user will see
  73. ** menu items.  This approach to deciding what enable/ disable state a menu
  74. ** item has the advantage of concentrating all the decision-making in one
  75. ** routine, as opposed to being spread throughout the application.  Other
  76. ** application designs may take a different approach that is just as valid.
  77. */
  78.  
  79. #pragma segment Menu
  80. void    AdjustMenus(void)
  81. {
  82.     WindowPtr        window;
  83.     MenuHandle        menu;
  84.     short            numWindows, itemToCheck;
  85.     Boolean            maxWindows, menuEnabled, redrawMenuBar;
  86.     FileRecHndl        frHndl;
  87.     static Boolean    editMenuEnabled = true;
  88.     static Boolean    opponentMenuEnabled = true;
  89.  
  90.     redrawMenuBar = false;
  91.     window = FrontWindow();
  92.     menu = GetMHandle(mFile);
  93.  
  94.     EnableItem(menu, iNew);                /* Set these for the no-windows state. */
  95.     EnableItem(menu, iOpen);
  96.     if (MaxBlock() < 0x10000L) {
  97.         if (CompactMem(0x10000L) < 0x10000L) {
  98.             DisableItem(menu, iNew);    /* Running low on RAM. */
  99.             DisableItem(menu, iOpen);
  100.         }
  101.     }
  102.  
  103.     DisableItem(menu, iClose);
  104.     DisableItem(menu, iSave);
  105.     DisableItem(menu, iSaveAs);
  106.     DisableItem(menu, iDuplicate);
  107.     DisableItem(menu, iPageSetup);
  108.     DisableItem(menu, iPrint);
  109.         /* Quit is always hilited. */
  110.  
  111.  
  112.  
  113.     if (IsDAWindow(window)) {
  114.         DisableItem(menu, iNew);    /* DAs don't get to do a new. */
  115.         DisableItem(menu, iOpen);    /* DAs don't get to do an open. */
  116.         EnableItem(menu, iClose);    /* Let DAs do a close from the menu. */
  117.     }
  118.  
  119.     if (IsAppWindow(window)) {
  120.         numWindows = GetWindowCount(false, false);
  121.         maxWindows = (numWindows < kMaxNumWindows);
  122.         EnableOrDisableItem(menu, iNew, maxWindows);
  123.         EnableOrDisableItem(menu, iOpen, maxWindows);
  124.             /* Allow a new and open only if max number of files not reached. */
  125.  
  126.         EnableItem(menu, iClose);
  127.         EnableOrDisableItem(menu, iSave, AppWindowDirty(window));
  128.         EnableItem(menu, iSaveAs);
  129.         EnableItem(menu, iDuplicate);
  130.  
  131.         EnableItem(menu, iPageSetup);
  132.         EnableItem(menu, iPrint);
  133.     }
  134.  
  135.  
  136.  
  137.     menu = GetMHandle(mEdit);
  138.     if (IsDAWindow(window)) {        /* A desk accessory may need edit menu. */
  139.         menuEnabled = true;
  140.         EnableItem(menu, iUndo);
  141.         EnableItem(menu, iCut);
  142.         EnableItem(menu, iCopy);
  143.         EnableItem(menu, iPaste);
  144.         EnableItem(menu, iClear);
  145.     } else {
  146.         CTEEditMenu(&menuEnabled, mEdit, iUndo, iCut);
  147.     }
  148.     if (editMenuEnabled != menuEnabled) {
  149.         redrawMenuBar = true;
  150.         if (editMenuEnabled = menuEnabled)
  151.              EnableItem(menu, 0);
  152.         else DisableItem(menu, 0);
  153.     }
  154.  
  155.  
  156.     menu = GetMHandle(mGame);                /* Start by disabling everything. */
  157.     menuEnabled = false;
  158.     DisableItem(menu, iConfigureGame);
  159.     DisableItem(menu, iInvertBoard);
  160.     DisableItem(menu, iArrangeBoard);
  161.     DisableItem(menu, iPlayOnePlayer);
  162.     DisableItem(menu, iPlayTwoPlayer);
  163.  
  164.     CheckItem(menu, iArrangeBoard, false);
  165.     CheckItem(menu, iPlayOnePlayer, false);
  166.     CheckItem(menu, iPlayTwoPlayer, false);
  167.  
  168.     if (IsAppWindow(window)) {
  169.  
  170.         frHndl = (FileRecHndl)GetWRefCon(window);
  171.  
  172.         if (!(*frHndl)->fileState.readOnly) {
  173.  
  174.             itemToCheck = iPlayOnePlayer + (*frHndl)->doc.twoPlayer;
  175.             if ((*frHndl)->doc.arrangeBoard) itemToCheck = iArrangeBoard;
  176.             CheckItem(menu, itemToCheck, true);
  177.  
  178.             if (!(*frHndl)->doc.arrangeBoard) EnableItem(menu, iConfigureGame);
  179.             EnableItem(menu, iInvertBoard);
  180.             EnableItem(menu, iArrangeBoard);
  181.             EnableItem(menu, iPlayOnePlayer);
  182.             menuEnabled = true;
  183.  
  184.             if (gHasAppleEvents) {                /* Now the 7.0 goodie... */
  185.                 EnableItem(menu, iPlayTwoPlayer);
  186.             }
  187.         }
  188.     }
  189.     if (opponentMenuEnabled != menuEnabled) {
  190.         redrawMenuBar = true;
  191.         if (opponentMenuEnabled = menuEnabled)
  192.              EnableItem(menu, 0);
  193.         else DisableItem(menu, 0);
  194.     }
  195.  
  196.     if (redrawMenuBar) DrawMenuBar();
  197. }
  198.  
  199.  
  200.  
  201. /*****************************************************************************/
  202.  
  203.  
  204.  
  205. /* This function either enables or disables a menu item. */
  206.  
  207. #pragma segment Menu
  208. void    EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
  209. {
  210.     if (enable) EnableItem(menu, item);
  211.     else        DisableItem(menu, item);
  212. }
  213.  
  214.  
  215.  
  216. /*****************************************************************************/
  217.  
  218.  
  219.  
  220. /* This is called when an item is chosen from the menu bar (after calling
  221. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  222. ** It is good to have both the result of MenuSelect and MenuKey go to one
  223. ** routine like this to keep everything organized.
  224. */
  225.  
  226. #pragma segment Menu
  227. void    DoMenuCommand(long menuResult)
  228. {
  229.     short        menuID;            /* The resource ID of the selected menu. */
  230.     short        menuItem;        /* The item number of the selected menu. */
  231.     Str255        daName;
  232.     short        daRefNum;
  233.     FileRecHndl    frHndl, newFrHndl;
  234.     OSErr        err;
  235.     WindowPtr    window;
  236.     short        itemHit;
  237.     DialogPtr    dlg;
  238.  
  239.     if (window = FrontWindow())
  240.         frHndl = (FileRecHndl)GetWRefCon(window);
  241.             /* frHndl is valid only if it is one of our windows. */
  242.  
  243.     menuID = HiWord(menuResult);    /* Use macros for efficiency to get */
  244.     menuItem = LoWord(menuResult);    /* menu item number and menu number. */
  245.  
  246.     switch (menuID) {
  247.  
  248.         case mApple:
  249.             switch (menuItem) {
  250.                 case iAbout:    /* Bring up alert for About. */
  251.                     Alert(rAboutAlert, nil);
  252.                     break;
  253.                 default:        /* All non-About items in this menu are DAs. */
  254.                     GetItem(GetMHandle(mApple), menuItem, daName);
  255.                     daRefNum = OpenDeskAcc(daName);
  256.                     break;
  257.             }
  258.             break;
  259.  
  260.         case mFile:
  261.             switch (menuItem) {
  262.                 case iNew:
  263.                     err = AppNewDocument(&frHndl);
  264.                     if (!err) {
  265.                         if (err = AppNewWindow(frHndl, nil))
  266.                             AppDisposeDocument(frHndl);
  267.                     }
  268.                     if (err) Alert(rErrorAlert, nil);
  269.                     break;
  270.                 case iOpen:
  271.                     err = AppOpenDocument(&frHndl, nil, fsRdWrPerm);
  272.                     if (!err) {
  273.                         if (err = AppNewWindow(frHndl, nil))
  274.                             AppDisposeDocument(frHndl);
  275.                     }
  276.                     if ((err) && (err != userCanceledErr))
  277.                         Alert(rErrorAlert, nil);
  278.                     break;
  279.                 case iClose:
  280.                     CloseOneWindow(window, iClose);
  281.                     break;
  282.                 case iSave:
  283.                     err = AppSaveDocument(frHndl, window, iSave);
  284.                     if ((err) && (err != userCanceledErr))
  285.                         Alert(rErrorAlert, nil);
  286.                     break;
  287.                 case iSaveAs:
  288.                     err = AppSaveDocument(frHndl, window, iSaveAs);
  289.                     if ((err) && (err != userCanceledErr))
  290.                         Alert(rErrorAlert, nil);
  291.                     break;
  292.                 case iDuplicate:
  293.                     err = AppDuplicateDocument(frHndl, &newFrHndl);
  294.                     if (!err) {
  295.                         if (err = AppNewWindow(newFrHndl, nil))
  296.                             AppDisposeDocument(newFrHndl);
  297.                     }
  298.                     if (err) Alert(rErrorAlert, nil);
  299.                     break;
  300.                 case iPageSetup:
  301.                     DoSetCursor(&QD(arrow));
  302.                     PresentStyleDialog(frHndl);
  303.                     break;
  304.                 case iPrint:
  305.                     DoSetCursor(&QD(arrow));
  306.                     err = noErr;
  307.                     if (!(*frHndl)->doc.printRecValid)
  308.                         err = PresentStyleDialog(frHndl);
  309.                     if (!err) {
  310.                         err = AppPrintDocument(frHndl, true, true);
  311.                         AppPrintDocument(nil, false, false);
  312.                     }
  313.                     if ((err) && (err != userCanceledErr)) Alert(rErrorAlert, nil);
  314.                     break;
  315.                 case iQuit:
  316.                     gQuitApplication = CloseAllWindows();
  317.                     break;
  318.             }
  319.             break;
  320.  
  321.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  322.             if (IsAppWindow(window)) {
  323.                 switch (menuItem) {
  324.                     case iUndo:
  325.                         CTEUndo();
  326.                         break;
  327.                     case iCut:
  328.                     case iCopy:
  329.                     case iPaste:
  330.                     case iClear:
  331.                         CTEClipboard(menuItem - iCut + 2);
  332.                         break;
  333.                 }
  334.             }
  335.             else SystemEdit(menuItem - 1);
  336.             break;
  337.  
  338.         case mGame:
  339.             switch (menuItem) {
  340.                 case iConfigureGame:
  341.                     DoSetCursor(&QD(arrow));
  342.                     DoConfigureGame(frHndl);
  343.                     break;
  344.                 case iInvertBoard:
  345.                     (*frHndl)->doc.invertBoard ^= 1;
  346.                     SetPort(window);
  347.                     ImageDocument(frHndl, true);
  348.                     DrawTime(frHndl);
  349.                     break;
  350.                 case iArrangeBoard:
  351.                 case iPlayOnePlayer:
  352.                 case iPlayTwoPlayer:
  353.                     DoSetCursor(&QD(arrow));
  354.                     if ((menuItem == iArrangeBoard) && ((*frHndl)->doc.numGameMoves)) {
  355.                         if (dlg = GetCenteredDialog(rArrangeWarning, nil, nil, (WindowPtr)-1L)) {
  356.                             OutlineDialogItem(dlg, 1);
  357.                             ModalDialog((ModalFilterProcPtr)keyEquivFilter, &itemHit);
  358.                             DisposDialog(dlg);
  359.                             if (itemHit == 3) break;
  360.                         }
  361.                     }
  362.                     if (menuItem == iPlayTwoPlayer)
  363.                         SendGame(frHndl, kIsMove);
  364.                     else
  365.                         SetOpponentType(frHndl, menuItem - iPlayOnePlayer + kOnePlayer);
  366.                     break;
  367.  
  368.             }
  369.             break;
  370.  
  371.     }
  372.  
  373.     HiliteMenu(0);        /* Unhighlight what MenuSelect (or MenuKey) hilited. */
  374. }
  375.  
  376.  
  377.  
  378.